/**
 * Created by MD-3 on 2017/8/3.
 */


var deviceMarkerList = [];                    //数据以对象的形式保存在list中
function mDevice(id, type, name, lng, lat, marker, workState, childDevlist) {
    this.id             = id;                       //设备ID
    this.type           = type;                     //设备类型，0-中继，1-终端
    this.name           = name;                     //设备名称
    this.lng            = lng;                     //设备定位经度
    this.lat            = lat;                     //设备定位纬度
    this.marker         = marker;                   //覆盖图标
    this.workState      = workState;                //0-正常，1-离线，2-报警
    this.childDevlist   = childDevlist;             //相关联的上一级设备Id
}

function childDevice(id, type, name, lng, lat, radius, marker, polyline, circle, workState, alarmState, alarmlist) {
    this.id             = id;
    this.type           = type;
    this.name           = name;
    this.lng            = lng;
    this.lat            = lat;
    this.radius         = radius;
    this.marker         = marker;
    this.polyline       = polyline;
    this.circle         = circle;
    this.workState      = workState;
    this.alarmState     = alarmState;
    this.alarmlist      = alarmlist;
}

function alarmObj(alarmId, alarmTime) {
    this.alarmId        = alarmId;
    this.alarmTime      = alarmTime;
}

function getPreDevById(id) {
    if (!id || 0 > id) {
        return;
    }
    var dev;
    $.each(deviceMarkerList, function (idx, device) {
        if (id == device.id) {
            dev = device;
            return true;
        }
    });
    return dev;
}

function getDevById(id) {
    if (!id || 0 > id) {
        return;
    }
    var dev;
    $.each(deviceMarkerList, function (idx, device) {
        if (id == device.id) {
            dev = device;
            return true;
        }
        $.each(device.childDevlist, function (jdx, childDev) {
            if (id == childDev.id) {
                dev = childDev;
                return true;
            }
        });
    });
    return dev;
}

function getDevByName(name) {
    if (!name || ""==name) {
        return;
    }
    var dev;
    $.each(deviceMarkerList, function (idx, device) {
        if (name == device.name) {
            dev = device;
            return true;
        }
        $.each(device.childDevlist, function (jdx, childDev) {
            if (name == childDev.name) {
                dev = childDev;
                return true;
            }
        });
    });
    return dev;
}



function ajaxUpdate() {
    var rtmInfoList = deviceList_mock;
    if(!rtmInfoList || 0>rtmInfoList.length){
        return;
    }

    var preDev, childDev;
    $.each(rtmInfoList, function(i, rtmInfo){
            var rtmInfo = rtmInfoList[i]
            preDev = new mDevice();
            preDev.id           = rtmInfo.id;
            preDev.type         = rtmInfo.type;
            preDev.name         = rtmInfo.name;
            preDev.lng          = rtmInfo.position.lng;
            preDev.lat          = rtmInfo.position.lat;
            preDev.workState    = rtmInfo.workState;
            preDev.childDevlist = [];
            //preDev.childDevlist = rtmInfo.childDevlist || [];

            $.each(rtmInfo.childDevList, function (j, rtmInfoChild) {
                childDev = new childDevice();
                childDev.id         = rtmInfoChild.id;
                childDev.type       = rtmInfoChild.type;
                childDev.name       = rtmInfoChild.name;
                childDev.lng        = rtmInfoChild.position.lng;
                childDev.lat        = rtmInfoChild.position.lat;
                childDev.radius     = rtmInfoChild.radius;
                childDev.workState  = rtmInfoChild.workState;
                childDev.alarmState = rtmInfoChild.alarmState;
                childDev.alarmlist  = rtmInfoChild.alarmlist;
                preDev.childDevlist.push(childDev);
            });
            deviceMarkerList.push(preDev);
    });

    addOverlay();                   //点标记

    //showDeviceMarkerList()
}








function showDeviceMarkerList(){
    for(var i in deviceMarkerList) {
        var device = deviceMarkerList[i]
        showdeviceMarker(device);
    }
}

function showdeviceMarker(device) {
    console.log("\n");
    console.log("id:"+device.id);
    console.log("type:"+device.type);
    console.log("name:"+device.name);
    console.log("lng:"+device.lng);
    console.log("lat:"+device.lat);
    console.log("workState:"+device.workState);
    console.log("childDevlist:"+device.childDevlist.length);
    for(var i in device.childDevlist){
        var childDev = device.childDevlist[i];
        console.log("      child-id:"+childDev.id);
        console.log("      child-name:"+childDev.name);
        console.log("      child-workState:"+childDev.workState);
        console.log("      child-alarmState:"+childDev.alarmState);
    }
}




